<html>
<head><meta charset="utf-8"><title>Socket2 [MaybeUninit&lt;u8&gt;] to [&lt;u8&gt;] · t-libs · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/index.html">t-libs</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Socket2.20.5BMaybeUninit.3Cu8.3E.5D.20to.20.5B.3Cu8.3E.5D.html">Socket2 [MaybeUninit&lt;u8&gt;] to [&lt;u8&gt;]</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="220589201"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Socket2%20%5BMaybeUninit%3Cu8%3E%5D%20to%20%5B%3Cu8%3E%5D/near/220589201" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thomas de Zeeuw <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Socket2.20.5BMaybeUninit.3Cu8.3E.5D.20to.20.5B.3Cu8.3E.5D.html#220589201">(Dec 21 2020 at 14:31)</a>:</h4>
<p>Not sure this the right place for this, I'm new to Zullip.</p>
<p>I'm looking for help with reviewing <a href="https://github.com/rust-lang/socket2/pull/161">https://github.com/rust-lang/socket2/pull/161</a>. The important part is that it casts a <code>&amp;mut [u8]</code> to <code>&amp;mut [MaybeUninit&lt;u8&gt;]</code>, which I know is unsound, but calls a function with a documented promise to not write initialised bytes to the slice (and directly calls the <code>recv</code> system call). So the question is: is that undefined behaviour?</p>



<a name="220595624"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Socket2%20%5BMaybeUninit%3Cu8%3E%5D%20to%20%5B%3Cu8%3E%5D/near/220595624" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Socket2.20.5BMaybeUninit.3Cu8.3E.5D.20to.20.5B.3Cu8.3E.5D.html#220595624">(Dec 21 2020 at 15:34)</a>:</h4>
<p>there's no <em>UB</em> in that cast, the unsoundness arises from other code misbehaving</p>



<a name="220595709"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Socket2%20%5BMaybeUninit%3Cu8%3E%5D%20to%20%5B%3Cu8%3E%5D/near/220595709" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Socket2.20.5BMaybeUninit.3Cu8.3E.5D.20to.20.5B.3Cu8.3E.5D.html#220595709">(Dec 21 2020 at 15:34)</a>:</h4>
<p>so if you have a way to ensure that <em>everyone</em> who gets access to that <code>&amp;mut [MaybeUninit&lt;u8&gt;]</code> follows the rules (i.e., doesn't de-initialized things), then you are good.</p>



<a name="220595818"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Socket2%20%5BMaybeUninit%3Cu8%3E%5D%20to%20%5B%3Cu8%3E%5D/near/220595818" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Socket2.20.5BMaybeUninit.3Cu8.3E.5D.20to.20.5B.3Cu8.3E.5D.html#220595818">(Dec 21 2020 at 15:35)</a>:</h4>
<p>That PR opens an interesting question though -- what happens when uninitialized memory is passed to syscalls? I am not aware of a proper account for this.^^</p>



<a name="220595969"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Socket2%20%5BMaybeUninit%3Cu8%3E%5D%20to%20%5B%3Cu8%3E%5D/near/220595969" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Socket2.20.5BMaybeUninit.3Cu8.3E.5D.20to.20.5B.3Cu8.3E.5D.html#220595969">(Dec 21 2020 at 15:36)</a>:</h4>
<p>and these are not even syscalls directly, right? there is some C code that is executed first?<br>
so how does that C code feel about working with uninit data? can we rely on this not causing trouble?</p>



<a name="220597298"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Socket2%20%5BMaybeUninit%3Cu8%3E%5D%20to%20%5B%3Cu8%3E%5D/near/220597298" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thomas de Zeeuw <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Socket2.20.5BMaybeUninit.3Cu8.3E.5D.20to.20.5B.3Cu8.3E.5D.html#220597298">(Dec 21 2020 at 15:48)</a>:</h4>
<p>I assume libc, whatever the implementation, doesn't write uninitialised bytes, but there aren't any guarantees in the documentation. It does document (in recv/read etc) that it writes valid bytes up to the amount returned, but again doesn't guarantee it doesn't change bytes after the written part. In other words I think with <code>let n = libc::recv(&amp;mut buf)</code>, <code>buf[..n]</code> are valid bytes, but nothing is guaranteed for <code>buf[n..]</code>.</p>



<a name="220600212"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Socket2%20%5BMaybeUninit%3Cu8%3E%5D%20to%20%5B%3Cu8%3E%5D/near/220600212" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Socket2.20.5BMaybeUninit.3Cu8.3E.5D.20to.20.5B.3Cu8.3E.5D.html#220600212">(Dec 21 2020 at 16:13)</a>:</h4>
<p>In my code I use <code>fn read(buf: &amp;mut [MaybeUninit&lt;u8&gt;]) -&gt; Result&lt;&amp;mut [u8]&gt;</code> which returns the part of the buffer that has been initialized.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>